package com.xuecheng.system.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.CompanyApproveDto;
import com.xuecheng.commons.model.dto.CompanyDto;
import com.xuecheng.commons.model.dto.CompanyListDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.model.vo.PageResponseResult;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.system.domain.Company;
import com.xuecheng.system.domain.User;
import com.xuecheng.system.mapper.CompanyMapper;
import com.xuecheng.system.service.CompanyService;
import com.xuecheng.system.service.UserService;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 教育机构 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-29
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    //机构资料提交
    @Override
    public ResponseResult saveCompany(CompanyDto dto) {
        //1.非空判断状态
        if (StrUtil.isBlank(dto.getName()) || StrUtil.isBlank(dto.getLinkname()) || StrUtil.isBlank(dto.getHandPhotos())) {
            throw new BusinessException(ErrorCode.NOTFOUND);
        }
        //1.1电话号码判断
        LambdaQueryWrapper<Company> qw = new LambdaQueryWrapper<>();
        qw.eq(StrUtil.isNotBlank(dto.getPhone()), Company::getPhone, dto.getPhone());
        long count = this.count(qw);
        if (count > 0) {
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //2.dto转java
        Company company = BeanHelper.copyProperties(dto, Company.class);
        //todo redis验证码
        //3.补充字段
        company.setApprovalStatus(CourseConstants.AUDIT_UNPAST_STATUS);//未提交
        company.setCreateDate(DateUtil.date());//创建时间
        company.setOrgType("c");//设置机构
        company.setStatus(-1);//设置状态
        //4.保存数据库
        this.save(company);
        //5.返回
        return ResponseResult.okResult();
    }

    //机构/个人组织列表
    @Override
    public ResponseResult findCompanyPage(CompanyListDto dto, PageRequest params) {
        //1.构建分页
        Page<Company> page = new Page<>(params.getPageNo(), params.getPageSize());
        //1.1 按,切割
        List<String> stringList = Arrays.asList(dto.getApprovalStatus().split(","));
        //2.构建条件
        LambdaQueryWrapper<Company> qw = new LambdaQueryWrapper<>();
        qw.in(Company::getApprovalStatus, stringList);//状态
        //3.执行分页
        page = this.page(page, qw);
        //4.转vo
        List<Company> companyList = page.getRecords();
        List<CompanyVo> voList = BeanHelper.copyWithCollection(companyList, CompanyVo.class);
        for (CompanyVo companyVo : voList) {
            companyVo.setCompanyId(companyVo.getId());
        }
        //5.返回结果
        return PageResponseResult.okResult(page.getTotal(), voList);
    }


    //机构审核
    @Override
    public ResponseResult approve(CompanyApproveDto dto) {
        //1.根据id查询机构
        Company company = this.getById(dto.getCompanyId());
        //2.补全信息
        company.setApprovalStatus(CourseConstants.AUDIT_COMMIT_STATUS);//已通过
        company.setStatus(-1);//状态改为-1
        company.setApprovalComment(dto.getApprovalComment());//审核意见
        company.setApprovalDate(DateUtil.date());//审核日期
        company.setApprovalNums(company.getApprovalNums() + 1);//审核次数+1
        //3.保存数据库
        this.updateById(company);
        //4.审核通过创建一个新的用户
        if (company.getApprovalStatus().equals(CourseConstants.AUDIT_COMMIT_STATUS)) {
            User user = new User();
            if (user == null) {
                user.setPhone(company.getPhone());//手机号
                String md5 = SecureUtil.md5("123456");
                user.setPassword(md5);//密码
                user.setName(company.getLinkname());//用户名
                user.setCompanyId(company.getId());//企业id
                user.setCompanyName(company.getName());//企业名字
                user.setUtype("platform");//平台
                user.setCreateDate(DateUtil.date());//创建时间u
                user.setStatus(1);//状态
                //保存数据库
                userService.save(user);
            }
        }
        //4.返回
        return ResponseResult.okResult();
    }
}
